﻿using System;
using System.Runtime.CompilerServices;
using UnityEditor;
using UnityEngine;

public class USUpgradePaths : Editor
{
    public static int CurrentVersionNumber = 9;
    public static UpgradeFunction[] UpgradeFunctions = new UpgradeFunction[] { new UpgradeFunction(USUpgradePaths.RunUpgradePathEmpty), new UpgradeFunction(USUpgradePaths.RunUpgradePathEmpty), new UpgradeFunction(USUpgradePaths.RunUpgradePathEmpty), new UpgradeFunction(USUpgradePaths.RunUpgradePathPre1_1), new UpgradeFunction(USUpgradePaths.RunUpgradePath1_1To1_11), new UpgradeFunction(USUpgradePaths.RunUpgradePath1_11To1_15), new UpgradeFunction(USUpgradePaths.RunUpgradePath1_15To1_16), new UpgradeFunction(USUpgradePaths.RunUpgradePath1_16To1_17), new UpgradeFunction(USUpgradePaths.RunUpgradePath1_17To1_171), new UpgradeFunction(USUpgradePaths.RunUpgradePath1_171To1_174) };
    public static string[] VersionNumbers = new string[] { "Pre-1.1", "Pre-1.1", "Pre-1.1", "V1.1", "V1.11", "V1.15", "V1.16", "V1.17", "V1.171", "V1.174" };

    private static bool RunUpgradePath1_11To1_15(USSequencer sequence)
    {
        sequence.Version = 5;
        USWindow.CreateAndAttachObserver(sequence);
        return true;
    }

    private static bool RunUpgradePath1_15To1_16(USSequencer sequence)
    {
        sequence.Version = 6;
        return true;
    }

    private static bool RunUpgradePath1_16To1_17(USSequencer sequence)
    {
        sequence.Version = 7;
        foreach (USTimelineContainer container in sequence.TimelineContainers)
        {
            foreach (USTimelineBase base2 in container.Timelines)
            {
                USTimelineObserver observer = base2 as USTimelineObserver;
                if (observer != null)
                {
                    foreach (USObserverKeyframe keyframe in observer.observerKeyframes)
                    {
                        if (keyframe.observer == null)
                        {
                            keyframe.observer = observer;
                        }
                    }
                }
            }
        }
        return true;
    }

    private static bool RunUpgradePath1_171To1_174(USSequencer sequence)
    {
        sequence.Version = 9;
        int num = 0;
        foreach (USTimelineContainer container in sequence.TimelineContainers)
        {
            bool flag = false;
            foreach (USTimelineBase base2 in container.Timelines)
            {
                USTimelineObserver observer = base2 as USTimelineObserver;
                if (observer != null)
                {
                    flag = true;
                }
            }
            if (flag)
            {
                container.Index = -1;
            }
            else
            {
                container.Index = num;
                num++;
            }
        }
        return true;
    }

    private static bool RunUpgradePath1_17To1_171(USSequencer sequence)
    {
        sequence.Version = 8;
        foreach (USTimelineContainer container in sequence.TimelineContainers)
        {
            foreach (USTimelineBase base2 in container.Timelines)
            {
                USTimelineObserver observer = base2 as USTimelineObserver;
                if (observer != null)
                {
                    foreach (USTimelineObserver.USTimelineObserverKeyframe keyframe in observer.keyframes)
                    {
                        if (keyframe != null)
                        {
                            USObserverKeyframe item = ScriptableObject.CreateInstance<USObserverKeyframe>();
                            item.observer = keyframe.observer;
                            item.FireTime = keyframe.FireTime;
                            item.camera = keyframe.camera;
                            item.fired = false;
                            item.prevActiveState = keyframe.prevActiveState;
                            observer.observerKeyframes.Add(item);
                        }
                    }
                    observer.keyframes.Clear();
                }
                USTimelineProperty property = base2 as USTimelineProperty;
                if (property != null)
                {
                    foreach (USPropertyInfo info in property.Properties)
                    {
                        if (info.curves0 != null)
                        {
                            USInternalCurve curve = ScriptableObject.CreateInstance<USInternalCurve>();
                            curve.Duration = sequence.Duration;
                            curve.UnityAnimationCurve = info.curves0;
                            info.curves.Add(curve);
                            while (info.curves0.keys.Length > 0)
                            {
                                info.curves0.RemoveKey(0);
                            }
                        }
                        if (info.curves1 != null)
                        {
                            USInternalCurve curve2 = ScriptableObject.CreateInstance<USInternalCurve>();
                            curve2.Duration = sequence.Duration;
                            curve2.UnityAnimationCurve = info.curves1;
                            info.curves.Add(curve2);
                            while (info.curves1.keys.Length > 0)
                            {
                                info.curves1.RemoveKey(0);
                            }
                        }
                        if (info.curves2 != null)
                        {
                            USInternalCurve curve3 = ScriptableObject.CreateInstance<USInternalCurve>();
                            curve3.Duration = sequence.Duration;
                            curve3.UnityAnimationCurve = info.curves2;
                            info.curves.Add(curve3);
                            while (info.curves2.keys.Length > 0)
                            {
                                info.curves2.RemoveKey(0);
                            }
                        }
                        if (info.curves3 != null)
                        {
                            USInternalCurve curve4 = ScriptableObject.CreateInstance<USInternalCurve>();
                            curve4.Duration = sequence.Duration;
                            curve4.UnityAnimationCurve = info.curves3;
                            info.curves.Add(curve4);
                            while (info.curves3.keys.Length > 0)
                            {
                                info.curves3.RemoveKey(0);
                            }
                        }
                    }
                }
            }
        }
        return true;
    }

    private static bool RunUpgradePath1_1To1_11(USSequencer sequence)
    {
        sequence.Version = 4;
        return true;
    }

    private static bool RunUpgradePathEmpty(USSequencer sequence)
    {
        sequence.Version = 2;
        return true;
    }

    private static bool RunUpgradePathPre1_1(USSequencer sequence)
    {
        sequence.Version = 3;
        return true;
    }

    public static bool RunUpgradePaths(USSequencer sequence)
    {
        if (sequence.Version == CurrentVersionNumber)
        {
            return false;
        }
        if (sequence.Version > CurrentVersionNumber)
        {
            Debug.LogWarning("You are using an older version of uSequencer, or you have a sequence in your scene from an older version, there is no guarantee this will work. Please upgrade uSequencer", sequence);
            return false;
        }
        if (CurrentVersionNumber >= VersionNumbers.Length)
        {
            Debug.LogError("We've made a mistake with the version numbers, this should never happen.");
            return false;
        }
        if (CurrentVersionNumber >= UpgradeFunctions.Length)
        {
            Debug.LogError("We've made a mistake with the upgrade functions, this should never happen.");
            return false;
        }
        if (VersionNumbers.Length != UpgradeFunctions.Length)
        {
            Debug.LogError("We've made a mistake with the version numbers or the Upgrade Functions, this should never happen.");
            return false;
        }
        while (sequence.Version < CurrentVersionNumber)
        {
            int index = sequence.Version + 1;
            if (!UpgradeFunctions[index](sequence))
            {
                return false;
            }
            Debug.Log("Upgraded Sequence : " + sequence.name + " To " + VersionNumbers[index], sequence);
        }
        Debug.LogWarning("Your Sequence : " + sequence.name + " has been automatically upgraded, don't forget to save your scene.", sequence);
        return true;
    }

    public delegate bool UpgradeFunction(USSequencer sequence);
}

